package com.outsourcing.bottle.util;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.annotation.SuppressLint;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ListView;

/**
 * 
 * @author 06peng
 * 
 */
public class Utility {

	
	private static final String TAG = Utility.class.getSimpleName();
	/** apikey is required http://developers.aviary.com/ */
	public static final String API_KEY = "xxxxx";
	
	public static void setListViewHeightBasedOnChildren(ListView listView) {
		Adapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		params.height += 60;
		listView.setLayoutParams(params);
	}
	
	public static boolean isExternalStorageAvilable() {
		String state = Environment.getExternalStorageState();
		if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
			return true;
		}
		return false;
	}
	
	/**
	 * Return a new image file. Name is based on the current time. Parent folder will be the one created with createFolders
	 * 
	 * @return
	 * @see #createFolders()
	 */
	@SuppressLint("NewApi")
	public static File getNextFileName() {
		
		File baseDir = Environment.getExternalStorageDirectory();

//		if ( android.os.Build.VERSION.SDK_INT < 8 ) {
//			baseDir = Environment.getExternalStorageDirectory();
//		} else {
//			baseDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES );
//		}

		if ( baseDir == null ) return Environment.getExternalStorageDirectory();

		Log.d( TAG, "Pictures folder: " + baseDir.getAbsolutePath() );
		File aviaryFolder = new File( baseDir, AppContext.EDITIMG_PATH );
		if(aviaryFolder.exists()){
			return  new File( aviaryFolder, "upload.jpeg" );	
		}else {
			aviaryFolder.mkdir();
			return  new File( aviaryFolder, "upload.jpeg" );
		}
	}
	
	
	public static String getTime(Date date) {
		TimeZone t = TimeZone.getTimeZone("GMT+08:00");// 获取东8区TimeZone
		Calendar calendar = Calendar.getInstance(t);
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int min = calendar.get(Calendar.MINUTE);
		int ss = calendar.get(Calendar.SECOND);

		String time = year + "-" + (month < 10 ? "0" : "") + month + '-'
				+ (day < 10 ? "0" : "") + day + ' ' + (hour < 10 ? "0" : "")
				+ hour + ':' + (min < 10 ? "0" : "") + min + ":"
				+ (ss < 10 ? "0" : "") + ss;
		return time;
	}

	public static String getDate() {
		TimeZone t = TimeZone.getTimeZone("GMT+08:00");// 获取东8区TimeZone
		Calendar calendar = Calendar.getInstance(t);
		calendar.setTimeInMillis(System.currentTimeMillis());
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		String date = year + "-" + (month < 10 ? "0" : "") + month + '-'
				+ (day < 10 ? "0" : "") + day;
		return date;
	}
	
}
